package com.nf.health.app.activity.my.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.nf.health.app.R;
import com.nf.health.app.activity.my.bean.MyHealthyTestCompeteModel;
import com.nf.health.app.adapter.ViewHolder;
import com.nf.health.app.customview.MyExpandableListView;
import com.nf.health.app.utils.LogUtil;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/29/029.
 */

public class MyQuestionExpandableAdapter  extends BaseExpandableListAdapter {
    public  List<MyHealthyTestCompeteModel.ListBean> list;
    private Context context;

    public  MyQuestionExpandableAdapter( List<MyHealthyTestCompeteModel.ListBean> list,Context context){
        this.context = context;
        this.list = list;
    }
    @Override
    public int getGroupCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
       Map<Integer, String> answers = list.get(groupPosition).getHashMap();
        return answers == null ? 0 : answers.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).getHashMap();

    }

    @Override
    public int getChildType(int groupPosition, int childPosition) {
        return super.getChildType(groupPosition, childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        ViewHolder holder = ViewHolder.get(parent, convertView, R.layout.item_risk_evaluation2, groupPosition);
        holder.setText(R.id.tv_question_title, (groupPosition+1)+". "+list.get(groupPosition).getTitle());
        holder.getView(R.id.ll_titleSub).setClickable(false);
        if(list.get(groupPosition).getType().equals("1")){
            ((ImageView)holder.getView(R.id.iv)).setImageResource(R.drawable.iv_single_question);
        }else{
            ((ImageView)holder.getView(R.id.iv)).setImageResource(R.drawable.iv_multy_question);
        }

        return holder.getConvertView();
    }

    /**
     * 展开所有
     * @param lv_expandable
     */
    public void  expandGroupMyList(MyExpandableListView lv_expandable){
        for (int i = 0; i < list.size(); i++) {
            lv_expandable.expandGroup(i);
        }
    }
    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder holder = ViewHolder.get(parent, convertView, R.layout.item_evaluation_answer, childPosition);
        CheckBox tv_title = holder.getView(R.id.tv_title);
        Map<Integer, String> answerMap = (Map<Integer, String>) getChild(groupPosition, childPosition);
        String type = list.get(groupPosition).getType();
        if(!TextUtils.isEmpty(type)){
            if(type.equals("2")){//多选
                for(Map.Entry<Integer, String> entry : answerMap.entrySet()) {
                    if (entry.getKey() == childPosition) {
                        String slectAnswer = list.get(groupPosition).getSelect();
                        if (!TextUtils.isEmpty(slectAnswer)) {//有多个答案
                            if (slectAnswer.contains(",")) {
                                String answer[] = slectAnswer.split(",");
                                if (slectAnswer.contains(entry.getValue())) {//答案包含当前选项
                                    tv_title.setChecked(true);
                                } else {
                                    tv_title.setChecked(false);
                                }
                            } else {

                                if (slectAnswer.contains(entry.getValue())) {//答案包含当前选项
                                    tv_title.setChecked(true);
                                } else {
                                    tv_title.setChecked(false);
                                }

                                break;
                            }
                        }
                        tv_title.setText(entry.getValue());
                        break;

                    }

                }
               /* String slectAnswer = list.get(groupPosition).getSelect();
                if(!TextUtils.isEmpty(slectAnswer)){
                    if(slectAnswer.contains(",")){
                        String answer[] = slectAnswer.split(",");
                        for(Map.Entry<Integer, String> entry : answerMap.entrySet()){
                            if(entry.getKey() == childPosition){
                                for (int i = 0; i < answer.length; i++) {
                                    String singleAnswer = answer[i];
                                    if(entry.getValue().equals(singleAnswer)){
                                        tv_title.setChecked(true);
                                    }else{
                                        tv_title.setChecked(false);
                                    }
                                }
                            }
                            tv_title.setText(entry.getValue());
                        }

                    }else{//只有一个答案
                        for(Map.Entry<Integer, String> entry : answerMap.entrySet()){
                            if(entry.getKey() == childPosition){
                                if(entry.getValue().equals(list.get(groupPosition).getSelect())){
                                    tv_title.setChecked(true);
                                }else{
                                    tv_title.setChecked(false);
                                }
                                tv_title.setText(entry.getValue());
                                break;
                            }
                        }
                    }
                }*/
            }else{//单选
                for(Map.Entry<Integer, String> entry : answerMap.entrySet()){
                    if(entry.getKey() == childPosition){
                        if(entry.getValue().equals(list.get(groupPosition).getSelect())){
                            tv_title.setChecked(true);
                        }else{
                            tv_title.setChecked(false);
                        }
                        tv_title.setText(entry.getValue());
                        break;
                    }

                }
            }
        }
        LogUtil.e("yuyahao","entry.getValue():  " + list.get(groupPosition).getSelect());
        return holder.getConvertView();
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
			/*
			 * 很重要：实现ChildView点击事件，必须返回true
			 * 子布局需要加：android:descendantFocusability="blocksDescendants"
			 */
        return true;
    }

}